﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace EzFrameWork
{
    public class EzObjPoolMgr : Singleton<EzObjPoolMgr>
    {
        //存储
        private Dictionary<string, EzObjPoolRoot> ObjPoolDic;
        private Dictionary<Type, object> ClassObjectPoolDic;
        GameObject ezObjPoolMgr;

        public void Init()
        {
            ObjPoolDic = new Dictionary<string, EzObjPoolRoot>();
            Debug.Log("Init");
        }

        public void ClearInit()
        {
            Debug.Log("ClearInit");
            if (ObjPoolDic!=null)
            {
                ObjPoolDic.Clear();
            }
        }
        public EzObjPoolMgr()
        {
            ClassObjectPoolDic = new Dictionary<Type, object>();
            Init();
        }
        
        public void AddObjPool(string RootPoolName, EzObjPoolRoot ezObjPoolRoot)
        {
            if (!ObjPoolDic.ContainsKey(RootPoolName))
            {
                ObjPoolDic.Add(RootPoolName, ezObjPoolRoot);
            }
            else
            {
                Debug.Log("有这个:" + RootPoolName + "的缓存池根节点了");
            }
        }

        public int ObjPoolCount()
        {
            return ObjPoolDic.Count;
        }

        public void CreateNewPoolRoot(string AssetName, int defultCount = 10)
        {
            string[] temp = AssetName.Split('/');
            string name;
            if (temp.Length>0)
            {
                name = temp[temp.Length - 1];
            }
            else
            {
                name = AssetName;
            }

            GameObject RootPoolObj = new GameObject(name);
            if (GameObject.Find("EzObjPoolMgr") == null)
            {
                ezObjPoolMgr = new GameObject("EzObjPoolMgr");
                ezObjPoolMgr.AddComponent<EzObjPool>();
            }
            else
            {
                ezObjPoolMgr = GameObject.Find("EzObjPoolMgr");
            }
            RootPoolObj.transform.parent = ezObjPoolMgr.transform;
            EzObjPoolRoot root = RootPoolObj.AddComponent<EzObjPoolRoot>();
            AddObjPool(name, root);
            root.Instance(AssetName, name, defultCount);
        }
            
        //从资源池中请求物体   RootPoolName：请求资源的池名字
        public GameObject GetGameObjectFromPool(string AssetPath)
        {
            if (ObjPoolDic.ContainsKey(AssetPath))
            {
                return ObjPoolDic[AssetPath].GetGameObjectFromPool();
            }
            else
            {
                Debug.Log("没有名字叫" + AssetPath + "的缓存池根节点");
                return null;
            }
        }

        //物体回收到缓存池中
        public void PushGameObjectToPool(string RootPoolName, GameObject obj)
        {
            if (ObjPoolDic.ContainsKey(RootPoolName))
            {
                ObjPoolDic[RootPoolName].PushGameObjectToPool(obj);
            }
            else
            {
                Debug.Log("没有名字叫" + RootPoolName + "的缓存池根节点");
            }
        }
        
        //创建类对象池 创建完成后外面可以保存ClassObjectPool<T> 然后Spawn 和 Recycle 来创建和回收类对象
        public ClassObjectPool<T> GetOrCreateClassPool<T>(int maxcount) where T : class, new()
        {
            System.Type type = typeof(T);
            object outobj = null;
            if (!ClassObjectPoolDic.TryGetValue(type, out outobj) || outobj == null)
            {
                ClassObjectPool<T> newPool = new ClassObjectPool<T>(maxcount);
                ClassObjectPoolDic.Add(type, newPool);
            }

            return outobj as ClassObjectPool<T>;
        }

        //从对象池中取泛型的T对象
        public T NewClassObjectFromPool<T>(int maxcount) where T : class, new()
        {
            ClassObjectPool<T> newPool = new ClassObjectPool<T>(maxcount);
            if (newPool == null)
            {
                return null;
            }
            return newPool.Spawn(true);
        }
    }
}
